package com.ruoyi.zsyh.controller.applet.agent;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CommonRpc;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.RedisUtil;
import com.ruoyi.zsyh.domain.SysDtArea;
import com.ruoyi.zsyh.domain.ZsyhAgentAddress;
import com.ruoyi.zsyh.domain.ZsyhAgentInfo;
import com.ruoyi.zsyh.service.ISysDtAreaService;
import com.ruoyi.zsyh.service.IZsyhAgentAddressService;
import com.ruoyi.zsyh.service.IZsyhAgentInfoService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 大区会长档案Controller
 * 
 * @author yuexl
 * @date 2021-12-10
 */
@Controller
@RequestMapping("/applet/zsyh/agentAddress")
public class ZsyhAgentAddressAppletController extends BaseController
{

    @Autowired
    private IZsyhAgentAddressService zsyhAgentAddressService;

    @Autowired
    private ISysDtAreaService sysDtAreaService;

    /**
     * 获取省市县
     * @param pid
     * @return
     */
    @RequestMapping("/getAreaByPid")
    @ResponseBody
    public AjaxResult getAreaByPid(String pid){
        pid = EmptyUtils.isEmpty(pid)?"0":pid;
        SysDtArea area = new SysDtArea();
        area.setAreaParentId(pid);
        return AjaxResult.success("获取成功", sysDtAreaService.selectSysDtAreaListOrderByInitials(area));
    }

    /**
     * 获取省市县 根据省级会长id
     * @param pid
     * @return
     */
    @RequestMapping("/getAreaByAgentId")
    @ResponseBody
    public AjaxResult getAreaByAgentId(String pid, String agentId, String type){
        pid = EmptyUtils.isEmpty(pid)?"0":pid;
        SysDtArea area = new SysDtArea();
        area.setAreaParentId(pid);

        Map map = new HashedMap();
        map.put("agentType", type);
        map.put("agentId", agentId);
        area.setParams(map);
        return AjaxResult.success("获取成功", sysDtAreaService.selectSysDtAreaListOrderByInitials(area));
    }

    /**
     * 获取省市县 按照字母排列
     * @param pid
     * @return
     */
    @RequestMapping("/getAreaListByPid")
    @ResponseBody
    public AjaxResult getAreaListByPid(String pid, String isRaker){
        pid = EmptyUtils.isEmpty(pid)?"0":pid;
        SysDtArea area = new SysDtArea();
        area.setAreaParentId(pid);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(isRaker))map.put("isRaker", isRaker);
        area.setParams(map);
        List<SysDtArea> areaList = sysDtAreaService.selectSysDtAreaListOrderByInitials(area);
        List<SysDtArea> listA = new ArrayList<>();List<SysDtArea> listB = new ArrayList<>();List<SysDtArea> listC = new ArrayList<>();
        List<SysDtArea> listD = new ArrayList<>();List<SysDtArea> listE = new ArrayList<>();List<SysDtArea> listF = new ArrayList<>();
        List<SysDtArea> listG = new ArrayList<>();List<SysDtArea> listH = new ArrayList<>();List<SysDtArea> listI = new ArrayList<>();
        List<SysDtArea> listJ = new ArrayList<>();List<SysDtArea> listK = new ArrayList<>();List<SysDtArea> listL = new ArrayList<>();
        List<SysDtArea> listM = new ArrayList<>();List<SysDtArea> listN = new ArrayList<>();List<SysDtArea> listO = new ArrayList<>();
        List<SysDtArea> listP = new ArrayList<>();List<SysDtArea> listQ = new ArrayList<>();List<SysDtArea> listR = new ArrayList<>();
        List<SysDtArea> listS = new ArrayList<>();List<SysDtArea> listT = new ArrayList<>();List<SysDtArea> listU = new ArrayList<>();
        List<SysDtArea> listV = new ArrayList<>();List<SysDtArea> listW = new ArrayList<>();List<SysDtArea> listX = new ArrayList<>();
        List<SysDtArea> listY = new ArrayList<>();List<SysDtArea> listZ = new ArrayList<>();
        for(SysDtArea a:areaList){
            String initials = a.getAreaInitials();
            switch (initials){
                case "A":listA.add(a);break;                case "B":listB.add(a);break;                case "C":listC.add(a);break;
                case "D":listD.add(a);break;                case "E":listE.add(a);break;                case "F":listF.add(a);break;
                case "G":listG.add(a);break;                case "H":listH.add(a);break;                case "I":listI.add(a);break;
                case "J":listJ.add(a);break;                case "K":listK.add(a);break;                case "L":listL.add(a);break;
                case "M":listM.add(a);break;                case "N":listN.add(a);break;                case "O":listO.add(a);break;
                case "P":listP.add(a);break;                case "Q":listQ.add(a);break;                case "R":listR.add(a);break;
                case "S":listS.add(a);break;                case "T":listT.add(a);break;                case "U":listU.add(a);break;
                case "V":listV.add(a);break;                case "W":listW.add(a);break;                case "X":listX.add(a);break;
                case "Y":listY.add(a);break;                case "Z":listZ.add(a);break;
            }
        }
        Map mapA = new HashMap();
        mapA.put("letter", "A"); mapA.put("data", listA);
        Map mapB = new HashMap();
        mapB.put("letter", "B"); mapB.put("data", listB);
        Map mapC = new HashMap();
        mapC.put("letter", "C"); mapC.put("data", listC);
        Map mapD = new HashMap();
        mapD.put("letter", "D"); mapD.put("data", listD);
        Map mapE = new HashMap();
        mapE.put("letter", "E"); mapE.put("data", listE);
        Map mapF = new HashMap();
        mapF.put("letter", "F"); mapF.put("data", listF);
        Map mapG = new HashMap();
        mapG.put("letter", "G"); mapG.put("data", listG);
        Map mapH = new HashMap();
        mapH.put("letter", "H"); mapH.put("data", listH);
        Map mapI = new HashMap();
        mapI.put("letter", "I"); mapI.put("data", listI);
        Map mapJ = new HashMap();
        mapJ.put("letter", "J"); mapJ.put("data", listJ);
        Map mapK = new HashMap();
        mapK.put("letter", "K"); mapK.put("data", listK);
        Map mapL = new HashMap();
        mapL.put("letter", "L"); mapL.put("data", listL);
        Map mapM = new HashMap();
        mapM.put("letter", "M"); mapM.put("data", listM);
        Map mapN = new HashMap();
        mapN.put("letter", "N"); mapN.put("data", listN);
        Map mapO = new HashMap();
        mapO.put("letter", "O"); mapO.put("data", listO);
        Map mapP = new HashMap();
        mapP.put("letter", "P"); mapP.put("data", listP);
        Map mapQ = new HashMap();
        mapQ.put("letter", "Q"); mapQ.put("data", listQ);
        Map mapR = new HashMap();
        mapR.put("letter", "R"); mapR.put("data", listR);
        Map mapS = new HashMap();
        mapS.put("letter", "S"); mapS.put("data", listS);
        Map mapT = new HashMap();
        mapT.put("letter", "T"); mapT.put("data", listT);
        Map mapU = new HashMap();
        mapU.put("letter", "U"); mapU.put("data", listU);
        Map mapV = new HashMap();
        mapV.put("letter", "V"); mapV.put("data", listV);
        Map mapW = new HashMap();
        mapW.put("letter", "W"); mapW.put("data", listW);
        Map mapX = new HashMap();
        mapX.put("letter", "X"); mapX.put("data", listX);
        Map mapY = new HashMap();
        mapY.put("letter", "Y"); mapY.put("data", listY);
        Map mapZ = new HashMap();
        mapZ.put("letter", "Z"); mapZ.put("data", listZ);
        List list = new ArrayList();
        list.add(mapA);        list.add(mapB);        list.add(mapC);        list.add(mapD);
        list.add(mapE);        list.add(mapF);        list.add(mapG);        list.add(mapH);
        list.add(mapI);        list.add(mapJ);        list.add(mapK);        list.add(mapL);
        list.add(mapM);        list.add(mapN);        list.add(mapO);        list.add(mapP);
        list.add(mapQ);        list.add(mapR);        list.add(mapS);        list.add(mapT);
        list.add(mapU);        list.add(mapV);        list.add(mapW);        list.add(mapX);
        list.add(mapY);        list.add(mapZ);

        return AjaxResult.success("获取成功", list);
    }

    /**
     * 添加收货地址
     * @return
     * @throws Exception
     */
    @RepeatSubmit
    @RequestMapping("/saveAgentAddress")
    @ResponseBody
    public AjaxResult saveAgentAddress(String agentId, String name, String phone, String pCode, String pName,String cCode, String cName, String aCode, String aName, String address)throws Exception
    {
        ZsyhAgentAddress agentAddress = new ZsyhAgentAddress();
        agentAddress.setId(IdWorker.getId());
        agentAddress.setAgentId(agentId);
        agentAddress.setAddressDetail(address);
        agentAddress.setAddressPrivinceName(pName);
        agentAddress.setAddressProvince(pCode);
        agentAddress.setAddressCity(cCode);
        agentAddress.setAddressCityName(cName);
        agentAddress.setAddressDistrict(aCode);
        agentAddress.setAddressDistrictName(aName);
        agentAddress.setAddressName(name);
        agentAddress.setAddressPhone(phone);

        ZsyhAgentAddress appletAddress = new ZsyhAgentAddress();
        appletAddress.setAgentId(agentId);
        List<ZsyhAgentAddress> list = zsyhAgentAddressService.selectZsyhAgentAddressList(appletAddress);
        if(list.size() == 0){
            agentAddress.setIsDefault("1");
        }
        zsyhAgentAddressService.insertZsyhAgentAddress(agentAddress);
        return success("操作成功");
    }

    /**
     *  编辑收货地址
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateAgentAddress")
    @ResponseBody
    public AjaxResult updateAgentAddress(String id, String name, String phone, String pCode, String pName,String cCode, String cName, String aCode, String aName, String address, String userPriceType)throws Exception
    {
        ZsyhAgentAddress agentAddress = new ZsyhAgentAddress();
        agentAddress.setId(id);
        agentAddress.setAddressDetail(address);
        agentAddress.setAddressPrivinceName(pName);
        agentAddress.setAddressProvince(pCode);
        agentAddress.setAddressCity(cCode);
        agentAddress.setAddressCityName(cName);
        agentAddress.setAddressDistrict(aCode);
        agentAddress.setAddressDistrictName(aName);
        agentAddress.setAddressName(name);
        agentAddress.setAddressPhone(phone);
        zsyhAgentAddressService.updateZsyhAgentAddress(agentAddress);

        return success("操作成功");
    }

    /**
     *  设为默认
     * @return
     * @throws Exception
     */
    @RequestMapping("/setDefault")
    @ResponseBody
    public AjaxResult setDefault(String agentId, String id)throws Exception
    {
        zsyhAgentAddressService.updateSetDefault(agentId, id);
        return success("操作成功");
    }

    /**
     * 【小程序】 删除收货地址
     * @return
     * @throws Exception
     */
    @RequestMapping("/delAgentAddress")
    @ResponseBody
    public AjaxResult delAgentAddress(String agentId, String id)throws Exception
    {
        ZsyhAgentAddress agentAddress = zsyhAgentAddressService.selectZsyhAgentAddressById(id);
        if("0".equals(agentAddress.getIsDefault())){
            zsyhAgentAddressService.deleteZsyhAgentAddressById(id);
        }else{
            ZsyhAgentAddress appletAddress = new ZsyhAgentAddress();
            appletAddress.setAgentId(agentId);
            List<ZsyhAgentAddress> list = zsyhAgentAddressService.selectZsyhAgentAddressList(appletAddress);
            if(list.size() == 1){
                return AjaxResult.error("操作失败，唯一地址无法删除");
            }else {
                String newId = "";
                for(ZsyhAgentAddress a:list){
                    if(!id.equals(a.getId())){
                        newId = a.getId();
                    }
                }
                zsyhAgentAddressService.deleteZsyhAgentAddressById(id);//删除地址
                ZsyhAgentAddress _user = new ZsyhAgentAddress();
                _user.setId(newId);
                _user.setIsDefault("1");
                zsyhAgentAddressService.updateZsyhAgentAddress(_user);//修改第一个地址为默认地址
            }
        }
        return AjaxResult.success("操作成功");
    }

    /**
     * 获取我的默认地址
     * @return
     * @throws Exception
     */
    @RequestMapping("/getAgentDefaultAddress")
    @ResponseBody
    public AjaxResult getAgentDefaultAddress(String agentId)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂无收货地址");
        }
        ZsyhAgentAddress appletAddress = new ZsyhAgentAddress();
        appletAddress.setAgentId(agentId);
        appletAddress.setIsDefault("1");
        List<ZsyhAgentAddress> list = zsyhAgentAddressService.selectZsyhAgentAddressList(appletAddress);
        if(list.size() > 0 ){
            return AjaxResult.success("操作成功", list.get(0));
        }else{
            return AjaxResult.error("暂无收货地址");
        }
    }

    /**
     * 获取我的地址信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getAgentAddressByAgentId")
    @ResponseBody
    public AjaxResult getAgentAddressByAgentId(String agentId)throws Exception
    {
        if(EmptyUtils.isEmpty(agentId)){
            return AjaxResult.error("暂未登录");
        }
        ZsyhAgentAddress appletAddress = new ZsyhAgentAddress();
        appletAddress.setAgentId(agentId);
        List<ZsyhAgentAddress> list = zsyhAgentAddressService.selectZsyhAgentAddressList(appletAddress);
        if(list.size() > 0 ){
            return AjaxResult.success("操作成功", list);
        }else{
            return AjaxResult.error("暂无收货地址");
        }
    }


    /**
     * 获取地址详情
     * @return
     * @throws Exception
     */
    @RequestMapping("/getAgentAddressById")
    @ResponseBody
    public AjaxResult getAgentAddressById(String id)throws Exception
    {
        return AjaxResult.success("操作成功", zsyhAgentAddressService.selectZsyhAgentAddressById(id));
    }

}
